package com.mc2.docapp.disabilityassessmenttool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_UL_muscle_power;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_ulmp_elbow;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_ulmp_shoulder;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_ulmp_wrist;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.CustDataType;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.InnerCalculations;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;

public class UpLimbMusclePower extends Activity {

    private static final int id = Activities.UPPER_LIMB_MUSCLE_POWER;
    public C_ulmp_shoulder c_ulmp_shoulder = new C_ulmp_shoulder();
    public C_ulmp_elbow c_ulmp_elbow = new C_ulmp_elbow();
    public C_ulmp_wrist c_ulmp_wrist = new C_ulmp_wrist();
    EditText shoulderFlexionLeft;
    EditText shoulderFlexionRight;
    EditText shoulderExtensionLeft;
    EditText shoulderExtensionRight;
    EditText shoulderInternalRotationLeft;
    EditText shoulderInternalRotationRight;
    EditText ShoulderExternalRotationLeft;
    EditText ShoulderExternalRotationRight;
    EditText shoulderAdductionLeft;
    EditText shoulderAdductionRight;
    EditText shoulderAbductionLeft;
    EditText shoulderAbductionRight;
    EditText elbowFlexionLeft;
    EditText elbowFlexionRight;
    EditText elbowExtensionLeft;
    EditText elbowExtensionRight;
    EditText elbowSupinationLeft;
    EditText elbowSupinationRight;
    EditText elbowPronationLeft;
    EditText elbowPronationRight;
    EditText wristPalmarflexionLeft;
    EditText wristPalmarflexionRight;
    EditText wristRadialDeviationLeft;
    EditText wristRadialDeviationRight;
    EditText wristPalmarDeviationLeft;
    EditText wristPalmarDeviationRight;
    EditText wristDorsiflexionLeft;
    EditText wristDorsiflexionRight;
    //db instance
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uplimb_muscle_power);

        shoulderFlexionLeft = (EditText) findViewById(R.id.shoulderFlexionLeft);
        shoulderFlexionRight = (EditText) findViewById(R.id.shoulderFlexionRight);
        shoulderExtensionLeft = (EditText) findViewById(R.id.shoulderExtensionLeft);
        shoulderExtensionRight = (EditText) findViewById(R.id.shoulderExtensionRight);
        shoulderInternalRotationLeft = (EditText) findViewById(R.id.shoulderInternalRotationLeft);
        shoulderInternalRotationRight = (EditText) findViewById(R.id.shoulderInternalRotationRight);
        ShoulderExternalRotationLeft = (EditText) findViewById(R.id.ShoulderExternalRotationLeft);
        ShoulderExternalRotationRight = (EditText) findViewById(R.id.ShoulderExternalRotationRight);
        shoulderAdductionLeft = (EditText) findViewById(R.id.shoulderAdductionLeft);
        shoulderAdductionRight = (EditText) findViewById(R.id.shoulderAdductionRight);
        shoulderAbductionLeft = (EditText) findViewById(R.id.shoulderAbductionLeft);
        shoulderAbductionRight = (EditText) findViewById(R.id.shoulderAbductionRight);
        elbowFlexionLeft = (EditText) findViewById(R.id.elbowFlexionLeft);
        elbowFlexionRight = (EditText) findViewById(R.id.elbowFlexionRight);
        elbowExtensionLeft = (EditText) findViewById(R.id.elbowExtensionLeft);
        elbowExtensionRight = (EditText) findViewById(R.id.elbowExtensionRight);
        elbowSupinationLeft = (EditText) findViewById(R.id.elbowSupinationLeft);
        elbowSupinationRight = (EditText) findViewById(R.id.elbowSupinationRight);
        elbowPronationLeft = (EditText) findViewById(R.id.elbowPronationLeft);
        elbowPronationRight = (EditText) findViewById(R.id.elbowPronationRight);
        wristPalmarflexionLeft = (EditText) findViewById(R.id.wristPalmarflexionLeft);
        wristPalmarflexionRight = (EditText) findViewById(R.id.wristPalmarflexionRight);
        wristRadialDeviationLeft = (EditText) findViewById(R.id.wristRadialDeviationLeft);
        wristRadialDeviationRight = (EditText) findViewById(R.id.wristRadialDeviationRight);
        wristPalmarDeviationLeft = (EditText) findViewById(R.id.wristPalmarDeviationLeft);
        wristPalmarDeviationRight = (EditText) findViewById(R.id.wristPalmarDeviationRight);
        wristDorsiflexionLeft = (EditText) findViewById(R.id.wristDorsiflexionLeft);
        wristDorsiflexionRight = (EditText) findViewById(R.id.wristDorsiflexionRight);
        initRealmDB();
        loadPrevData();
        sanitizeInput();


        Button saveNnextButton = (Button) findViewById(R.id.saveAndNext);
        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //HIP
                c_ulmp_shoulder.setFlexion(new CustDataType(Integer.parseInt(IfNull(shoulderFlexionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(shoulderFlexionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_shoulder.setExtension(new CustDataType(Integer.parseInt(IfNull(shoulderExtensionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(shoulderExtensionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_shoulder.setInternal_rotation(new CustDataType(Integer.parseInt(IfNull(shoulderInternalRotationRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(shoulderInternalRotationLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_shoulder.setExternal_rotation(new CustDataType(Integer.parseInt(IfNull(ShoulderExternalRotationRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(ShoulderExternalRotationLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_shoulder.setAdduction(new CustDataType(Integer.parseInt(IfNull(shoulderAdductionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(shoulderAdductionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_shoulder.setAbduction(new CustDataType(Integer.parseInt(IfNull(shoulderAbductionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(shoulderAbductionLeft.getText().toString(), "5")), 5d, 0, 0));


                c_ulmp_shoulder.calc(c_ulmp_shoulder);

                //ELBOW
                c_ulmp_elbow.setFlexion(new CustDataType(Integer.parseInt(IfNull(elbowFlexionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(elbowFlexionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_elbow.setExtension(new CustDataType(Integer.parseInt(IfNull(elbowExtensionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(elbowExtensionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_elbow.setSupination(new CustDataType(Integer.parseInt(IfNull(elbowSupinationRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(elbowSupinationLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_elbow.setPronation(new CustDataType(Integer.parseInt(IfNull(elbowPronationRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(elbowPronationLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_elbow.calc(c_ulmp_elbow);

                //WRIST
                //ankle foot
                c_ulmp_wrist.setDorsiflexion(new CustDataType(Integer.parseInt(IfNull(wristDorsiflexionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(wristDorsiflexionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_wrist.setPalmarflexion(new CustDataType(Integer.parseInt(IfNull(wristPalmarflexionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(wristPalmarflexionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_wrist.setRadial_deviation(new CustDataType(Integer.parseInt(IfNull(wristRadialDeviationRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(wristRadialDeviationLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_wrist.setPalmar_deviation(new CustDataType(Integer.parseInt(IfNull(wristPalmarDeviationRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(wristPalmarDeviationLeft.getText().toString(), "5")), 5d, 0, 0));

                c_ulmp_wrist.calc(c_ulmp_wrist);

                C_UL_muscle_power c_ul_muscle_power = new C_UL_muscle_power();
                c_ul_muscle_power.setC_ulmp_elbow(c_ulmp_elbow);
                c_ul_muscle_power.setC_ulmp_shoulder(c_ulmp_shoulder);
                c_ul_muscle_power.setC_ulmp_wrist(c_ulmp_wrist);

                InnerCalculations innerCalculations = new InnerCalculations();

                innerCalculations.setSummation_left(c_ul_muscle_power.getC_ulmp_shoulder().getInner_calculations().getSummation_left() +
                        c_ul_muscle_power.getC_ulmp_elbow().getInner_calculations().getSummation_left() +
                        c_ul_muscle_power.getC_ulmp_wrist().getInner_calculations().getSummation_left()
                );

                innerCalculations.setSummation_right(c_ul_muscle_power.getC_ulmp_shoulder().getInner_calculations().getSummation_right() +
                        c_ul_muscle_power.getC_ulmp_elbow().getInner_calculations().getSummation_right() +
                        c_ul_muscle_power.getC_ulmp_wrist().getInner_calculations().getSummation_right()
                );

                c_ul_muscle_power.setInnerCalculations(innerCalculations);

                NewPatient.finalDto.getClass_upperLimb_component().getClass_arm_component().setMuscle_power(c_ul_muscle_power);

                try {
//                    System.out.println(new ObjectMapper().writeValueAsString(c_active_rom));
                } catch (Exception e) {
                }
                System.out.println();
                c_ul_muscle_power.setId(id);
                realm.beginTransaction();
                realm.deleteAll();
                realm.insert(c_ul_muscle_power);
                realm.commitTransaction();


                Intent intent = new Intent(UpLimbMusclePower.this, router(UpLimbMusclePower.class));
                startActivity(intent);
            }
        });

        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });
        sanitizeInput();
    }

    public String IfNull(String value, String defaultValue) {
        if (value.isEmpty() || value == null) {
            return defaultValue;
        } else {
            return value;
        }
    }

    private void initRealmDB() {
        Realm.init(getApplicationContext());
        // realm = Realm.getDefaultInstance();
        RealmConfiguration config = new RealmConfiguration.Builder().name(id+".realm").build();
        realm = Realm.getInstance(config);
    }

    private void loadPrevData() {
        final RealmResults<C_UL_muscle_power> dtos = realm.where(C_UL_muscle_power.class).findAll();
//        .equalTo("id",id).findAll();
        System.out.println();
        try {
            //apply ui
            //
            //mapUI(new C_UL_muscle_power().strip(dtos.get(0)));
            //
            //realm.beginTransaction();
            //dtos.get(0).deleteFromRealm();
            //realm.commitTransaction();
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
            //no prev data found
        }
    }

    private void mapUI(C_UL_muscle_power oldData) {
        shoulderFlexionLeft.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getFlexion().getActual_left())));
        shoulderFlexionRight.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getFlexion().getActual_right())));

        shoulderExtensionLeft.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getExtension().getActual_left())));
        shoulderExtensionRight.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getExtension().getActual_right())));

        shoulderInternalRotationLeft.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getInternal_rotation().getActual_left())));
        shoulderInternalRotationRight.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getInternal_rotation().getActual_right())));

        ShoulderExternalRotationLeft.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getExternal_rotation().getActual_left())));
        ShoulderExternalRotationRight.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getExternal_rotation().getActual_right())));

        shoulderAdductionLeft.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getAdduction().getActual_left())));
        shoulderAdductionRight.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getAdduction().getActual_right())));

        shoulderAbductionLeft.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getAbduction().getActual_left())));
        shoulderAbductionRight.setText(String.valueOf((long) (oldData.getC_ulmp_shoulder().getAbduction().getActual_right())));

        elbowFlexionLeft.setText(String.valueOf((long) (oldData.getC_ulmp_elbow().getFlexion().getActual_left())));
        elbowFlexionRight.setText(String.valueOf((long) (oldData.getC_ulmp_elbow().getFlexion().getActual_right())));

        elbowExtensionLeft.setText(String.valueOf((long) (oldData.getC_ulmp_elbow().getExtension().getActual_left())));
        elbowExtensionRight.setText(String.valueOf((long) (oldData.getC_ulmp_elbow().getExtension().getActual_right())));

        elbowSupinationLeft.setText(String.valueOf((long) (oldData.getC_ulmp_elbow().getSupination().getActual_left())));
        elbowSupinationRight.setText(String.valueOf((long) (oldData.getC_ulmp_elbow().getSupination().getActual_right())));

        elbowPronationLeft.setText(String.valueOf((long) (oldData.getC_ulmp_elbow().getPronation().getActual_left())));
        elbowPronationRight.setText(String.valueOf((long) (oldData.getC_ulmp_elbow().getPronation().getActual_right())));

        wristPalmarflexionLeft.setText(String.valueOf((long) (oldData.getC_ulmp_wrist().getPalmarflexion().getActual_left())));
        wristPalmarflexionRight.setText(String.valueOf((long) (oldData.getC_ulmp_wrist().getPalmarflexion().getActual_right())));

        wristRadialDeviationLeft.setText(String.valueOf((long) (oldData.getC_ulmp_wrist().getRadial_deviation().getActual_left())));
        wristRadialDeviationRight.setText(String.valueOf((long) (oldData.getC_ulmp_wrist().getRadial_deviation().getActual_right())));

        wristPalmarDeviationLeft.setText(String.valueOf((long) (oldData.getC_ulmp_wrist().getPalmar_deviation().getActual_left())));
        wristPalmarDeviationRight.setText(String.valueOf((long) (oldData.getC_ulmp_wrist().getPalmar_deviation().getActual_right())));

        wristDorsiflexionLeft.setText(String.valueOf((long) (oldData.getC_ulmp_wrist().getDorsiflexion().getActual_left())));
        wristDorsiflexionRight.setText(String.valueOf((long) (oldData.getC_ulmp_wrist().getDorsiflexion().getActual_right())));


    }

    public void sanitizeInput() {
        shoulderFlexionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderFlexionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderExtensionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderExtensionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderInternalRotationLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderInternalRotationRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        ShoulderExternalRotationLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        ShoulderExternalRotationRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderAdductionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderAdductionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderAbductionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        shoulderAbductionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        elbowFlexionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        elbowFlexionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        elbowExtensionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        elbowExtensionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        elbowSupinationLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        elbowSupinationRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        elbowPronationLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        elbowPronationRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        wristPalmarflexionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        wristPalmarflexionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        wristRadialDeviationLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        wristRadialDeviationRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        wristPalmarDeviationLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        wristPalmarDeviationRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        wristDorsiflexionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        wristDorsiflexionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});


    }
}
